import 'package:appflutterhomeschool/api/api_service.dart';
import 'package:appflutterhomeschool/api/dio_utils.dart';
import 'package:appflutterhomeschool/common/common.dart';
import 'package:appflutterhomeschool/model/form_switch_model.dart';
import 'package:appflutterhomeschool/model/student_information_model.dart';
import 'package:appflutterhomeschool/provider/provider_widget.dart';
import 'package:appflutterhomeschool/ui/dialogs/prompt_dialog_widgets.dart';
import 'package:appflutterhomeschool/ui/widgets/my_app_bar.dart';
import 'package:appflutterhomeschool/ui/widgets/notice/switch_classes_widget.dart';
import 'package:appflutterhomeschool/ui/widgets/widgets.dart';
import 'package:appflutterhomeschool/view_model/notice/switch_classes_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';

///用户： ninetailedfox
///时间： 2020/6/14
///邮箱： 1037438704@qq.com
///功能： 班级管理页面
class SwitchClassesPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Scaffold(
        backgroundColor: Colors.grey[200],
        appBar: MyAppBar2(title: "班级管理"),
        body: ProviderWidget<SwitchClassesViewModel>(
          model: SwitchClassesViewModel(context),
          onModelReady: (value) => value.initView(),
          builder: (context, viewModel, child) {
            return SingleChildScrollView(
              child: Column(
                children: <Widget>[
                  ListView.builder(
                      shrinkWrap: true,
                      itemCount: viewModel.studentList.length,
                      physics: NeverScrollableScrollPhysics(),
                      itemBuilder: (context, index) {
                        StudentInformationModel studentModel =
                            viewModel.studentList[index];
                        return GestureDetector(
                          child: ShadowWidget(
                            externalPadding: EdgeInsets.only(
                              left: ScreenUtil().setWidth(35),
                              top: ScreenUtil().setWidth(35),
                              right: ScreenUtil().setWidth(35),
                            ),
                            withinPadding:
                                EdgeInsets.all(ScreenUtil().setWidth(51)),
                            childWidget: Column(
                              children: <Widget>[
                                SwitchRowWidget(
                                    lcassName: studentModel?.grade == null
                                        ? ""
                                        : "\t${studentModel?.grade ?? ""}",
                                    imageUrl: studentModel?.imageUrl,
                                    name: viewModel.identity
                                        ? studentModel.className
                                        : studentModel.userName,
                                    rightIcon: studentModel.choice),
                                //判断获取不同的UI
                                Visibility(
                                  visible: !viewModel.identity && studentModel.subjects != null,
                                  child:Container(
                                    padding: EdgeInsets.only(top: 5),
                                    child:  GridViewWidget(
                                      listImagUrl: studentModel?.subjects??[],
//                                    itemCont: studentModel.subjects.length,
                                    ),
                                  ),
                                )
                              ],
                            ),
                          ),
                          onTap: () {
                            viewModel.switchOnTap(index);
                          },
                        );
                      }),
                  Visibility(
                    visible: false,
                    child: SwitchClassesWidget(
                      textEditingController: viewModel.textEditingController,
                      onTap: () {
                        if (viewModel.textEditingController.text != "") {
                          PromptDialogWidgets(context).showButtomDialog(
                              identity: viewModel.identity,
                              gridList: viewModel.gridList,
                              onTap: (ValueModel valueModel) {
                                if (viewModel.identity) {
                                  DioUtils.getInstance()
                                      .post(ApiService.teacherJoinClass, {
                                    "token": Constant.getToken(),
                                    "cls_code":
                                        viewModel.textEditingController.text,
                                    "subject": valueModel.relationship,
                                  }, (successCallBack) {
                                    Fluttertoast.showToast(msg: "加入成功");
                                    viewModel.textEditingController.text = "";
                                    viewModel.initView();
                                  }, (errorCallBack) {});
                                } else {
                                  if (valueModel.relationship == "") {
                                    Fluttertoast.showToast(msg: "请输入学生姓名");
                                    return;
                                  }
                                  //班级代码
                                  DioUtils.getInstance()
                                      .post(ApiService.addStudent, {
                                    "token": Constant.getToken(),
                                    "cls_code":
                                        viewModel.textEditingController.text,
                                    "name": valueModel.name,
                                    "relation": valueModel.relationship,
                                  }, (successCallBack) {
                                    viewModel.textEditingController.text = "";
                                    Fluttertoast.showToast(
                                        msg: successCallBack["message"]);
                                    viewModel.initView();
                                  }, (errorCallBack) {});
                                }
                              });
                        }
                      },
                    ),
                  ),
                ],
              ),
            );
          },
        ),
      ),
      onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
    );
  }
}
